<?php

declare(strict_types=1);

namespace App\Controller\Admin\Dashboard;

use App\Entity\AdminUser;
use App\Entity\Log;
use App\Utils\Annotation\ApiAuthority;
use App\Utils\Annotation\ApiName;
use App\Utils\Helper\GeneralHelper;
use App\Utils\Traits\ControllerTrait;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

/**
 * Class AdminUserController
 * @package App\Controller\Admin\Dashboard
 * @Route("/admin/dashboard/workplace", name="admin-dashboard-workplace-")
 * @ApiName(name="工作台")
 */
class WorkplaceController extends AbstractController {
    use ControllerTrait;

    /**
     * @ApiName(name="工作台-项目")
     * @ApiAuthority(need="admin.dashboard.workplace")
     * @Route("/projects", methods={"GET"}, name="projects")
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     */
    public function getProjects () {
        $result = [];

        for ($i = 0; $i < 6; $i++) {
            $item = [
                'cover'       => GeneralHelper::getAvatar(),
                'title'       => '项目-'.($i + 1),
                'description' => '那是一种内在的东西，他们到达不了，也无法触及的',
            ];
            array_push($result, $item);
        }

        return json_success($result);
    }

    /**
     * @ApiName(name="工作台-日志")
     * @ApiAuthority(need="admin.dashboard.workplace")
     * @Route("/logs", methods={"GET"}, name="logs")
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     */
    public function getLogs () {
        $result = [];

        $logs = $this->getDoctrine()->getRepository(Log::class)->getWorkplaceTopLogs();
        if ($logs) {
            foreach ($logs as &$log) {
                $log['avatar'] = GeneralHelper::getAvatar($log['avatar']);
                $log['created_at'] = $log['created_at']->format('Y-m-d H:i:s');
            }
            $result = $logs;
        }

        return json_success($result);
    }

    /**
     * @ApiName(name="工作台-用户")
     * @ApiAuthority(need="admin.dashboard.workplace")
     * @Route("/users", methods={"GET"}, name="users")
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     */
    public function getUsers () {
        $result = [];

        $admin_users = $this->getDoctrine()->getRepository(AdminUser::class)->getWorkplaceTopAdminUsers(6);
        if ($admin_users) {
            foreach ($admin_users as &$admin_user) {
                $admin_user['avatar'] = GeneralHelper::getAvatar($admin_user['avatar']);
            }
            $result = $admin_users;
        }

        return json_success($result);
    }
}